

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Vector;

/******************************************************************************
 *sends anything to the Server
 ******************************************************************************/
class SendData extends Thread {

	ObjectOutputStream out;
	Vector<Object> data;
	Pledge pledge;
	ClientInfo ci;
	int index = -1;

	public SendData(ObjectOutputStream out, Vector<Object> data) {
		this.out = out;
		this.data = data;
	}

	public SendData(ObjectOutputStream out, Pledge data2) {
		this.out = out;
		this.pledge = data2;
		// TODO Auto-generated constructor stub
	}

	public SendData(ObjectOutputStream out, int index) {
		this.out = out;
		this.index = index;
	}

	public SendData(ObjectOutputStream out, ClientInfo clientInfo) {
		this.out = out;
		this.ci = clientInfo;
	}

	public void run() {
		
		//write out the pledge
		if(pledge != null)
		{
			try {
				System.err.println(pledge.getClass());
				out.writeObject(pledge);
				out.flush();
				return;
			} catch (IOException e) {
				e.printStackTrace();
				System.err.println(e);
			}
		}
		
		if(ci != null)
		{
			try {
				System.err.println("SENT CLIENTINFO: "+ci.getClass());
				out.writeObject(ci);
				out.flush();
				return;
			} catch (IOException e) {
				e.printStackTrace();
				System.err.println(e);
			}
		}
		
		if(index != -1) {
			try {
				System.err.println("SENT INDEX: "+index);
				out.writeObject(index);
				out.flush();
				return;
			} catch (IOException e) {
				e.printStackTrace();
				System.err.println(e);
			}
		}

		try {
			for (Object o : data) {
				if (int[][].class.isInstance(o)) {
					int[][] temp = (int[][]) o;
					o = null;
					o = new int[temp.length][temp[0].length];
					for (int i = 0; i < temp.length; i++) {
						for (int j = 0; j < temp[i].length; j++) {
							((int[][]) o)[i][j] = temp[i][j];
						}
					}
				}
				System.err.println("Sent: " + o.getClass());
				out.writeObject(o); 
				out.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println(e);
		}
	}
}